package com.example.demo02.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo02.dao.TcSpecialcarMapper;
import com.example.demo02.dao.TestMapper;
import com.example.demo02.dto.excelBean.BlackSpecialCarExcelBean;
import com.example.demo02.entity.TcParkpot;
import com.example.demo02.entity.TcSpecialcar;
import com.example.demo02.form.TcSpecialCarForm;
import com.example.demo02.mapstruct.ExcelBeanToEntityStruct;
import com.example.demo02.service.TcSpecialCarService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TcSpecialCarServiceImpl extends ServiceImpl<TcSpecialcarMapper, TcSpecialcar> implements TcSpecialCarService {

    @Resource
    ExcelBeanToEntityStruct excelBeanToEntityStruct;
    @Resource
    TestMapper testMapper;
    @Override
    @Transactional
    public void importBlackCar(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader excelReader = new ExcelReader(inputStream, 0);
            List<BlackSpecialCarExcelBean> excelBeans = excelReader.read(1, 3, BlackSpecialCarExcelBean.class);
            Map<String, String> map = testMapper.getAllParkpot().stream().collect(Collectors.toMap(TcParkpot::getParkpotName,TcParkpot::getParkpotid));
            excelBeans.forEach(e->e.setParkpotId(map.get(e.getParkpotName())));
            if(CollectionUtil.isEmpty(excelBeans))return;
            List<TcSpecialcar> collect = excelBeans.stream().map(e -> excelBeanToEntityStruct.toTcSpecialcar(e)).collect(Collectors.toList());
            this.saveBatch(collect);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void blackCarExport(TcSpecialCarForm tcSpecialCarForm, HttpServletRequest request, HttpServletResponse response) {

    }

}
